package pointshop;

import mcps.bukkit.pointsystem.PointSystem;

import mcps.bukkit.pointsystem.PointSystemAPI;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.server.PluginDisableEvent;
import org.bukkit.event.server.PluginEnableEvent;
import org.bukkit.plugin.Plugin;

import java.util.logging.Logger;

/**
 * Created by Administrator on 2014/11/11.
 */
public class Point_system implements Points{
    private Plugin plugin = null;
    private PointSystem ess = null;
    private static final Logger log = Logger.getLogger("Minecraft");
    public Point_system() {
    }

    public Point_system(Plugin plugin) {
        this.plugin = plugin;

        Bukkit.getServer().getPluginManager().registerEvents(new PointServerListener(this), plugin);

        if (this.ess == null)
        {
            Plugin essentials = plugin.getServer().getPluginManager().getPlugin("PointSystem");

            if ((essentials != null) && (essentials.isEnabled()))
            {
                this.ess = ((PointSystem)essentials);
                log.info(String.format("[%s][PointSystem] %s hooked.", new Object[] { plugin.getDescription().getName(), "PointSystem" }));
            }
        }
    }

    public boolean isEnabled()
    {
        if (this.ess == null) {
            return false;
        }
        return this.ess.isEnabled();
    }

    @Override
    public boolean payPoints(String playerName, int amount, String info) {
      boolean b=mcps.bukkit.pointsystem.PointSystemAPI.payPoints(playerName,amount,info);

        return b;
    }


    public class PointServerListener
            implements Listener
    {
        Point_system economy = null;

        public PointServerListener(Point_system economy) { this.economy = economy; }

        @EventHandler(priority=EventPriority.MONITOR)
        public void onPluginEnable(PluginEnableEvent event) {
            if (this.economy.ess == null) {
                Plugin essentials = event.getPlugin();

                if (essentials.getDescription().getName().equals("PointSystem")) {
                    this.economy.ess = ((PointSystem)essentials);

                }
            }
        }

        @EventHandler(priority=EventPriority.MONITOR)
        public void onPluginDisable(PluginDisableEvent event) {
            if ((this.economy.ess != null) && (event.getPlugin().getDescription().getName().equals("PointSystem")))
            {
                this.economy.ess = null;

            }
        }
    }
}
